﻿/// <reference path="../jquery.min.js" />
/// <reference path="../module/rsseasy.js" />
var smstoken;
$("#loginpage").load(function () {
    $(this).find("[ry-imgtoken]").click();
}).load();

$("#smsloginpage").load(function () {
    $('.getCode').click(function () {
        sendCode();
    })
    $('.register').click(function () {
        submitPassword();
    })
})
function sendCode() {
    if (!checkPhone($('.tel').val())) return false
    setTime();
    smsLogin();
}
function checkPhone(phone) {
    if (!(/^1(3|4|5|7|8|9)\d{9}$/.test(phone))) {
        alert('手机号码有误，请重填')
        return false;
    } else {
        return true;
    }
}
function setTime() {
    var _this = this;
    var countdown = $('.time').text();
    if (countdown == 0) {
        countdown = 60;
        $('.getCode').removeClass('hide');
        $('.time').addClass('hide');
    } else {
        countdown--;
        setTimeout(function() {
            _this.setTime()
        }, 1000)
        $('.getCode').addClass('hide');
        $('.time').removeClass('hide');
    }
    $('.time').text(countdown)
}
function smsLogin() {
    if ($('.imgcodess').val().length === 0) {
        alert('请填写验证码');
        return false;
    }
    let params = {
        telphone: $('.tel').val(),
        imgcode: $('.imgcodess').val(),
        imgtoken: RssApp.ImgToken,
    }
    let url = api.smsVerify;
    params.signtoken = makeSign(params);
    const x = {
        url: url,
        method: 'POST',
        data: params,
        header: {
            'content-type': 'application/x-www-form-urlencoded',
        }
    }
    request(x, function (res) {
        console.info(res)
        smstoken = res.smstoken
    })

}
function submitPassword(){
    let params={
        rsswhere: JSON.stringify({ "myid": localStorage.myid }),
        pwd:$('.pwd').val(),
        smscode: $('.sms-code').val(),
        smstoken: smstoken,
        telphone: $('.tel').val(),
    }
    let url = api.smsPwd;
    const x ={
        url: url,
        method: 'POST',
        data: params,
        header: {
            'content-type': 'application/x-www-form-urlencoded',
            'usertoken': localStorage.usertoken,
            'tokenmyid': localStorage.tokenmyid
        }
    }
    request(x, function (res) {
        if (!res.rsscode) {
            //alert('修改成功');
            location.href='#loginpage';
        } else {
            //alert('修改失败');
        }
    })
}